package com.ourteam.product.requirement.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractTreeBuilderController;
import com.ourteam.frame.web.AbstractTreeBuilder;
import com.ourteam.frame.web.TreeParamModel;
import com.ourteam.frame.web.TreeViewNodeBean;
import com.ourteam.product.requirement.domain.ProductVersionBean;
import com.ourteam.product.requirement.service.IProductVersionService;
import com.ourteam.product.requirement.service.ProductVersionServiceFactory;

abstract public class AbstractProductDefineItemTreeBuilderAPI<T extends AbstractTreeBuilder>
		extends AbstractTreeBuilderController<T> {

	private static final IProductVersionService PRODUCT_VERSION_SERVICE = ProductVersionServiceFactory
			.getProductVersionService();

	@RequestMapping({ "/buildTree/{productVersionId}/" })
	@ResponseBody
	public List<TreeViewNodeBean> buildTree(@PathVariable long productVersionId,
			@ModelAttribute TreeParamModel treeParamModel, @RequestParam(defaultValue = "0") int expandLevel,
			@RequestParam(required = false) String selectedNodeId, @RequestParam(required = true) String requestId,
			HttpServletRequest request) throws Exception {

		ProductVersionBean productVersionBean = PRODUCT_VERSION_SERVICE.getProductVersionById(productVersionId);

		request.setAttribute("productDefineId", productVersionBean.getProductDefineId());

		request.setAttribute("productVersionId", productVersionId);

		return super.buildTree(treeParamModel, expandLevel, selectedNodeId, requestId, request);

	}
	
}
